//
//  MainController.swift
//  weibo
//
//  Created by mac on 16/8/3.
//  Copyright © 2016年 zl. All rights reserved.
//

import UIKit

class MainController: UITabBarController {
    
    @objc private func composeBtnClick () {
        
        print("点我")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
       
        
        addChildViewControllers()
        
        
        setupComposeButton()
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.bringSubviewToFront(composeBtn)
        

    }

    private lazy var composeBtn = UIButton(imageName: "tabbar_compose_icon_add", backgroundName: "tabbar_compose_button")
  

}

extension MainController {
    //添加按钮
   private func setupComposeButton () {
    
    tabBar.addSubview(composeBtn)
    
    let num = childViewControllers.count
    
    let w = tabBar.bounds.width / CGFloat(num) - 1
    
    composeBtn.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
    
    composeBtn.addTarget(self, action: "composeBtnClick", forControlEvents: .TouchUpInside)
    }
    
    
    //添加子控件控制器
    private func addChildViewControllers() {
        
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(MessageViewController(), title: "信息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(MineViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(MoreViewController(), title: "个人", imageName: "tabbar_profile")
    }
    
    
    private func addChildViewController(vc : UITableViewController, title : String,imageName : String) {
        //创建tableview控制器
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //创建nav控制器
        let nav = UINavigationController(rootViewController: vc)

        addChildViewController(nav)
        
    }
    
}